//
//  NewsViewController.m
//  Qihuo
//
//  Created by Vencoo on 13-10-11.
//  Copyright (c) 2013年 Vencoo. All rights reserved.
//

#import "NewsCell.h"
#import "NewsViewController.h"
#import "MyTextField.h"
#import "UITableViewCell+new.h"
#import "NSDictionary+new.h"
#import "UIButton+MyButton.h"
#import "BlackbackgroundView.h"
#import "MyDetailViewController.h"

@interface NewsViewController ()
{
    ASIFormDataRequest *addNewsRequest;
    ASIFormDataRequest *deleteRequest;
    ASIFormDataRequest *editRequest;
    ASIFormDataRequest *queryRequest;
    ASIFormDataRequest *searchRequest;
    NSData *myData;
    int searchNumber;
    UITableView *mainTable;
    UITableView *smallTable;
    UITableView *departTable;
    
    UILabel *departmentLb;
    UIButton *departBtn;
    UIButton *departBtn1;
    BlackbackgroundView *blackView;
    UITextField *personTf;
    UITextField *newsTf;
    UILabel *departTf;
    UITextView *contentTv;
    UIImageView *contentView;
    UILabel *contentLb;
    UIImage *triangleImage;
    UIImage *triangleImage1;
    
    NSDictionary *queryNewsDic;
    //    NSDictionary *queryListDic;
    
    NSArray *departs;
    
    NSMutableArray *contents;
    NSMutableArray *crttimes;
    NSMutableArray *departmentids;
    NSMutableArray *ids;
    NSMutableArray *titles;
    NSMutableArray *crtusers;
    NSMutableArray *userids;
    NSMutableArray *newsInfos;
    NSMutableArray *labelArr;
    
    UIAlertView *titleAlertView;
    UIAlertView *userNameAlertView;
}

@end

@implementation NewsViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self addRightButton];
    searchNumber = [[[Appinfomation shareIstance].loginDic valueForKey:@"departmentid"]integerValue];
    titleAlertView = [[UIAlertView alloc]initWithTitle:@"警告" message:@"标题不能为空！" delegate:self cancelButtonTitle:@"填写标题" otherButtonTitles:nil, nil];
    userNameAlertView = [[UIAlertView alloc]initWithTitle:@"警告" message:@"创建人不能为空！" delegate:self cancelButtonTitle:@"填写创建人" otherButtonTitles:nil, nil];
    
    departs = @[@"保安部",@"运作部",@"工程部"];
    departBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    UIImage *image1 = [UIImage imageNamed:@"下拉-底框"];
    UIImage *image2 = [UIImage imageNamed:@"下拉-右框常态"];
    triangleImage = [UIImage imageNamed:@"下拉-按钮1"];
    triangleImage1 = [UIImage imageNamed:@"下拉-按钮2"];
    UIImage *subMitImage = [UIImage imageNamed:@"提交信息"];
    UIImage *chongImage = [UIImage imageNamed:@"重置"];
    UIImageView *backgroundView = [[UIImageView alloc]initWithImage:image1];
    backgroundView.userInteractionEnabled = YES;
    backgroundView.frame = CGRectMake(interval111, interval111 * 4, image1.size.width, image1.size.height);
    [self.view addSubview:backgroundView];
    
    departmentLb = [UILabel new];
    departmentLb.text = @"保安部";
    departmentLb.font = [UIFont systemFontOfSize:30];
    departmentLb.backgroundColor = [UIColor clearColor];
    departmentLb.textColor = [UIColor whiteColor];
    departmentLb.frame = CGRectMake(10, 0, image1.size.width - image2.size.width - 10,image2.size.height);
    [backgroundView addSubview:departmentLb];
    
    departBtn.frame = CGRectMake(departmentLb.frame.size.width + departmentLb.frame.origin.x                                                            ,departmentLb.frame.origin.y, image2.size.width, image2.size.height);
    [departBtn setImage:image2 forState:UIControlStateNormal];
    [departBtn addTarget:self action:@selector(openTable) forControlEvents:UIControlEventTouchUpInside];
    [backgroundView addSubview:departBtn];
    
    newsInfos = [NSMutableArray array];
    [self addSearch];
    self.title = @"公司新闻信息";
    queryNewsDic = [NSDictionary dictionary];
    //    queryListDic = [NSDictionary dictionary];
    
    contents = [NSMutableArray array];
    crttimes = [NSMutableArray array];
    departmentids = [NSMutableArray array];
    ids = [NSMutableArray array];
    titles = [NSMutableArray array];
    crtusers = [NSMutableArray array];
    userids = [NSMutableArray array];
    labelArr = [NSMutableArray array];
    [self loadData];
    
    UIImage *lineImage = [UIImage imageNamed:@"黑边框"];
    UIImage *whiteimage = [UIImage imageNamed:@"底框-白.png"];
    UIImageView *MyView = [[UIImageView alloc]initWithFrame:CGRectMake((scrennWidth - whiteimage.size.width)/2, 150, whiteimage.size.width, whiteimage.size.height)];
    MyView.image = whiteimage;
    [MyView setUserInteractionEnabled:YES];
    [self.view addSubview:MyView];
    
    UIImageView *lineView = [[UIImageView alloc]initWithImage:lineImage];
    lineView.frame = CGRectMake((whiteimage.size.width - lineImage.size.width)/2, (whiteimage.size.height - lineImage.size.height)/2, lineImage.size.width, lineImage.size.height);
    lineView.userInteractionEnabled = YES;
    [MyView addSubview:lineView];
    
    mainTable = [[UITableView alloc]initWithFrame:CGRectMake(2, 2, lineImage.size.width-6, lineImage.size.height-6) style:UITableViewStylePlain];
    mainTable.dataSource = self;
    mainTable.delegate = self;
    [lineView addSubview:mainTable];
    mainTable.tag = 100;
    //    [mainTable reloadData];
    UIImage *tableBackImage = [UIImage imageNamed:@"灰底下拉框"];
    UIImage *tableImage = [UIImage imageNamed:@"公司信息下拉-底框"];
    smallTable = [[UITableView alloc]initWithFrame:CGRectMake(backgroundView.frame.origin.x, backgroundView.frame.origin.y + backgroundView.frame.size.height, tableImage.size.width,tableImage.size.height) style:UITableViewStylePlain];
    smallTable.backgroundColor = [UIColor clearColor];
    smallTable.backgroundView = [[UIImageView alloc]initWithImage:tableImage];
    smallTable.dataSource = self;
    smallTable.delegate = self;
    [self.view addSubview:smallTable];
    smallTable.hidden = YES;
    blackView = [[BlackbackgroundView alloc]initWithFrame:CGRectMake(0, 0, scrennWidth, screenHeight)];
    [self.view addSubview:blackView];
    blackView.delegate = self;
    blackView.userInteractionEnabled = YES;
    blackView.hidden = YES;
    
    UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hiddenAlll)];
    [blackView addGestureRecognizer:gesture];
    
    UIImage *inputImage = [UIImage imageNamed:@"灰底输入框"];
    UIImage *titleImage = [UIImage imageNamed:@"新闻标题"];
    UIImage *contentImage = [UIImage imageNamed:@"新闻内容"];
    UILabel *titleLb = [UILabel new];
    titleLb.text = @"新建公司信息";
    titleLb.font = [UIFont systemFontOfSize:35];
    titleLb.backgroundColor = [UIColor clearColor];
    CGSize sz = [titleLb.text sizeWithFont:titleLb.font constrainedToSize:CGSizeMake(MAXFLOAT, 40)];
    titleLb.frame = CGRectMake((blackView.frame.size.width - sz.width)/2, 80, sz.width, sz.height);
    [blackView addSubview:titleLb];
    
    UILabel *personLb = [UILabel new];
    personLb.text = @"创建人：";
    personLb.font = [UIFont systemFontOfSize:25];
    personLb.backgroundColor = [UIColor clearColor];
    sz = [personLb.text sizeWithFont:personLb.font constrainedToSize:CGSizeMake(MAXFLOAT, 40)];
    personLb.frame = CGRectMake(145, titleLb.frame.origin.y + titleLb.frame.size.height + 55, sz.width, sz.height);
    [blackView addSubview:personLb];
    
    personTf = [[UITextField alloc]initWithFrame:CGRectMake(personLb.frame.origin.x + personLb.frame.size.width, personLb.frame.origin.y, inputImage.size.width, sz.height)];
    personTf.background = titleImage;
    personTf.text = [[Appinfomation shareIstance].loginDic valueForKey:@"username"];
    personTf.contentVerticalAlignment = 0;
    personTf.delegate = self;
    [blackView addSubview:personTf];
    
    UILabel *newsLb = [UILabel new];
    newsLb.text = @"新闻标题：";
    newsLb.font = [UIFont systemFontOfSize:25];
    sz = [newsLb.text sizeWithFont:newsLb.font constrainedToSize:CGSizeMake(MAXFLOAT, 40)];
    newsLb.backgroundColor = [UIColor clearColor];
    newsLb.frame = CGRectMake(personLb.frame.origin.x - 25, personLb.frame.origin.y + personLb.frame.size.height + interval111, sz.width, sz.height);
    [blackView addSubview:newsLb];
    
    newsTf = [[UITextField alloc]initWithFrame:CGRectMake(personTf.frame.origin.x, personTf.frame.origin.y + personTf.frame.size.height + interval111, titleImage.size.width, titleImage.size.height)];
    newsTf.background = titleImage;
    newsTf.contentVerticalAlignment = 0;
    newsTf.delegate = self;
    [blackView addSubview:newsTf];
    
    UILabel *departLb = [UILabel new];
    departLb.text = @"所属部门：";
    departLb.font = [UIFont systemFontOfSize:25];
    departLb.backgroundColor = [UIColor clearColor];
    departLb.frame = CGRectMake(newsLb.frame.origin.x, newsLb.frame.origin.y + newsLb.frame.size.height + interval111, sz.width, sz.height);
    [blackView addSubview:departLb];
    
    UIImageView *departView = [[UIImageView alloc]initWithImage:titleImage];
    departView.frame = CGRectMake(newsTf.frame.origin.x, newsTf.frame.origin.y + newsTf.frame.size.height + interval111, personTf.frame.size.width, personTf.frame.size.height);
    departView.userInteractionEnabled = YES;
    [blackView addSubview:departView];
    
    departTf = [[UILabel alloc]init];
    departTf.text = @"保安部";
    departTf.font = [UIFont systemFontOfSize:23];
    sz = [departTf.text sizeWithFont:departTf.font constrainedToSize:CGSizeMake(MAXFLOAT, 40)];
    departTf.backgroundColor = [UIColor clearColor];
    departTf.frame = CGRectMake(5, (departView.frame.size.height - sz.height)/2, sz.width, sz.height);
    [departView addSubview:departTf];
    
    departBtn1 = [UIButton buttonWithType:UIButtonTypeCustom];
    [departBtn1 setImage:triangleImage forState:UIControlStateNormal];
    [departBtn1 addTarget:self action:@selector(openDepart:) forControlEvents:UIControlEventTouchUpInside];
    departBtn1.frame = CGRectMake(inputImage.size.width - triangleImage.size.width, 0, triangleImage.size.width, triangleImage.size.height);
    [departView addSubview:departBtn1];
    
    contentLb = [UILabel new];
    contentLb.text = @"新闻内容：";
    contentLb.font = [UIFont systemFontOfSize:25];
    sz = [contentLb.text sizeWithFont:contentLb.font constrainedToSize:CGSizeMake(MAXFLOAT, 40)];
    contentLb.backgroundColor = [UIColor clearColor];
    contentLb.frame = CGRectMake(departLb.frame.origin.x, departLb.frame.origin.y + departLb.frame.size.height + interval111, sz.width, sz.height);
    [blackView addSubview:contentLb];
    
    contentView = [[UIImageView alloc]initWithImage:contentImage];
    contentView.frame = CGRectMake(newsTf.frame.origin.x, contentLb.frame.origin.y, contentImage.size.width, contentImage.size.height);
    contentView.userInteractionEnabled = YES;
    [blackView addSubview:contentView];
    
    contentTv = [[UITextView alloc]initWithFrame:CGRectMake(0, 0, contentImage.size.width, contentImage.size.height)];
    contentTv.backgroundColor = [UIColor clearColor];
    contentTv.font = [UIFont systemFontOfSize:25];
    contentTv.delegate = self;
    [contentView addSubview:contentTv];
    
    for (int i = 0; i<2; i++) {
        UIButton *downBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        downBtn.tag = i;
        switch (i) {
            case 0:
            {
                [downBtn setImage:subMitImage forState:UIControlStateNormal];
            }
                break;
                
            default:
            {
                [downBtn setImage:chongImage forState:UIControlStateNormal];
            }
                break;
        }
        downBtn.frame = CGRectMake((scrennWidth - chongImage.size.width*2)/3*(i+1)+chongImage.size.width * i, contentView.frame.size.height + contentView.frame.origin.y + 30, chongImage.size.width, chongImage.size.height);
        [downBtn addTarget:self action:@selector(clicked1:) forControlEvents:UIControlEventTouchUpInside];
        [blackView addSubview:downBtn];
    }
    //    UIImageView *backTableView = [[UIImageView alloc]initWithImage:tableBackImage];
    //    backTableView.userInteractionEnabled = YES;
    departTable = [[UITableView alloc]initWithFrame:CGRectMake(departView.frame.origin.x, departView.frame.origin.y + departView.frame.size.height, departView.frame.size.width, tableBackImage.size.height)];
    departTable.backgroundView = [[UIImageView alloc]initWithImage:tableBackImage];
    departTable.delegate = self;
    departTable.dataSource = self;
    [self.view addSubview:departTable];
    departTable.hidden = YES;
    
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardDidShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasHidden:) name:UIKeyboardDidHideNotification object:nil];
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == mainTable) {
        return newsInfos.count;
    }else {
        return departs.count;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == smallTable || tableView == departTable) {
        static NSString *identifier = @"myCell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (!cell) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        }
        cell.textLabel.text = [departs objectAtIndex:indexPath.row];
        cell.backgroundColor = [UIColor clearColor];
        if (tableView == smallTable) {
            [cell.textLabel setFont:[UIFont systemFontOfSize:30]];
            cell.textLabel.textColor = [UIColor whiteColor];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
        
    }else if ([tableView isEqual:mainTable]) {
        NewsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        cell.dateLb.text =@"";
        cell.titleLabel.text =@"";
        if (!cell) {
            cell = [[[NSBundle mainBundle]loadNibNamed:@"NewsCell" owner:self options:nil] lastObject];
        }
        cell.dateLb.text =[crttimes objectAtIndex:indexPath.row];
        cell.titleLabel.text =[titles objectAtIndex:indexPath.row];
        
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        
        return cell;
    }
    
    
    
    return nil;
}
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == mainTable) {
        return 53.4;
    }else if (tableView == smallTable){
        return 50;
    }else if (tableView == departTable){
        return 45;
    }
    return 0;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == mainTable) {
        MyDetailViewController *detView = [MyDetailViewController new];
        _ID = [ids objectAtIndex:indexPath.row];
        detView.delegate = self;
        [self.navigationController pushViewController:detView animated:YES];
    }else if (tableView == smallTable){
        smallTable.hidden = YES;
        mainTable.scrollEnabled = YES;
        departmentLb.text = [departs objectAtIndex:indexPath.row];
        [departBtn setImage: [UIImage imageNamed:@"下拉-右框常态"] forState:UIControlStateNormal];
        searchNumber = indexPath.row+1;
    }else if(tableView == departTable){
        [self hiddenAlll];
        departTf.text = [departs objectAtIndex:indexPath.row];
        NSLog(@"%d========",indexPath.row);
    }
}
#pragma mark - UITextField
- (void)textFieldDidBeginEditing:(UITextField *)textField{
    departTable.hidden = YES;
    [departBtn1 setImage:triangleImage forState:UIControlStateNormal];
    
}
#pragma mark - UITextViewDelegate
- (void)textViewDidBeginEditing:(UITextView *)textView
{
    departTable.hidden = YES;
    [UIView animateWithDuration:.2 animations:^{
        [departBtn1 setImage:triangleImage forState:UIControlStateNormal];
        blackView.frame = CGRectMake(0, -contentView.frame.size.height-50, scrennWidth, screenHeight);
    }];
}
- (void)textViewDidEndEditing:(UITextView *)textView
{
    [UIView animateWithDuration:.2 animations:^{
        blackView.frame = CGRectMake(0, 0, scrennWidth, screenHeight);
    }];
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if (range.location>=1300)
    {
        return  NO;
    }
    else
    {
        return YES;
    }
}
#pragma mark - keyboardNotification
CGSize keyboardSize;

- (void) keyboardWasShown:(NSNotification *) notif{
    NSDictionary *info = [notif userInfo];
    NSValue *value = [info objectForKey:UIKeyboardFrameBeginUserInfoKey];
    keyboardSize = [value CGRectValue].size;
    NSLog(@"keyBoard:%f", keyboardSize.width);  //216
    
    
}
- (void) keyboardWasHidden:(NSNotification *) notif{
    NSDictionary *info = [notif userInfo];
    NSValue *value = [info objectForKey:UIKeyboardFrameBeginUserInfoKey];
    CGSize keyboardSize = [value CGRectValue].size;
    [blackView setAnimationDuration:1];
//    [blackView startAnimating];
    
    NSLog(@"keyboardWasHidden keyBoard:%f", keyboardSize.width);
}


#pragma mark - button action

- (void)add:(UIButton*)btn
{
    blackView.hidden = NO;
    self.navigationController.navigationBar.hidden = YES;
    
}
//提交 重置
- (void)clicked1:(UIButton*)btn
{
    [self hiddenAlll];
    switch (btn.tag) {
        case 0:
        {
            
            if ([newsTf.text isEqualToString: @""]) {
                [titleAlertView show];
            }else if ([personTf.text isEqualToString:@""])
            {
                [userNameAlertView show];
            }else{
                NSString * url = [NSString stringWithFormat:addNews_IP,[newsTf.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],[contentTv.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],[[Appinfomation shareIstance].loginDic valueForKey:@"id"],[[Appinfomation shareIstance].loginDic valueForKey:@"id"],[personTf.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
                NSURL *newurl =  [[NSURL alloc] initWithString:[url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
                ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:newurl];
                [request setDelegate:self];
                [request setDidFinishSelector:@selector(requestDone:)];
                [request setDidFailSelector:@selector(requestWentWrong:)];
                [request startAsynchronous];
                
                newsTf.text = nil;
                personTf.text = [[Appinfomation shareIstance].loginDic valueForKey:@"username"];
                contentTv.text = nil;
            
                NSString *response = [addNewsRequest responseString];
                NSLog(@"response111:%@",response);
                blackView.hidden = YES;
                self.navigationController.navigationBar.hidden = NO;
            }
        }
            break;
            
        default:{
            newsTf.text = nil;
            personTf.text = nil;
            contentTv.text = nil;
        }
            break;
    }
}
- (void)deleteNews
{
    //    deleteRequest = [APhttpUtil builddeleteNews:@"1"];
    //    deleteRequest.delegate = self;
    //    [deleteRequest startAsynchronous];
}
-(void)editNews
{
    //    editRequest = [APhttpUtil buildEditNewsId:@"1" name:@"haha" content:@"nini" crtUser:@"aaa" userID:@"1"];
    //    editRequest.delegate = self;
    //    [editRequest startAsynchronous];
}

- (void)analyseData
{
    
}
- (void)openTable
{
    if (smallTable.hidden) {
        smallTable.hidden = NO;
        mainTable.scrollEnabled = NO;
        [departBtn setImage:[UIImage imageNamed:@"下拉-右框"] forState:UIControlStateNormal];
    }else{
        smallTable.hidden = YES;
        mainTable.scrollEnabled = YES;
        [departBtn setImage: [UIImage imageNamed:@"下拉-右框常态"] forState:UIControlStateNormal];
    }
}
- (void)openDepart:(UIButton*)btn
{
    [[[UIApplication sharedApplication]keyWindow]endEditing:YES];
    if (departTable.hidden) {
        departTable.hidden = NO;
        [btn setImage:triangleImage1 forState:UIControlStateNormal];
    }else{
        departTable.hidden = YES;
        [btn setImage:triangleImage forState:UIControlStateNormal];
    }
}
- (void)searchInfo:(UIButton*)btn
{
    [[[UIApplication sharedApplication]keyWindow]endEditing:YES];
    NSString * url = [NSString stringWithFormat:searchNews,self.seachBar.text,[NSString stringWithFormat:@"%d",searchNumber],@"1"];
    NSURL *emerUrl =  [[NSURL alloc] initWithString:[url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    searchRequest = [ASIHTTPRequest requestWithURL:emerUrl];
    [searchRequest setDelegate:self];
    [searchRequest setDidFinishSelector:@selector(requestDone1:)];
    [searchRequest setDidFailSelector:@selector(requestWentWrong:)];
    [searchRequest startAsynchronous];
}


- (void)hiddenAlll
{
    departTable.hidden = YES;
    [departBtn1 setImage:triangleImage forState:UIControlStateNormal];
    [[[UIApplication sharedApplication]keyWindow]endEditing:YES];
}
- (void)loadData
{
    NSURL *queryUrl = [NSURL URLWithString:queryNew_IP];
    queryRequest = [[ASIFormDataRequest alloc] initWithURL:queryUrl];
    
    [queryRequest setPostValue:[[Appinfomation shareIstance].loginDic valueForKey:@"departmentid" ] forKey:@"departmentid"];
    [queryRequest setPostValue:@"1" forKey:@"pageNo"];
    [queryRequest startSynchronous];
    //    NSLog(@"%@",[queryRequest responseData]);
    NSString *response = [queryRequest responseString];
    NSLog(@"response:%@",[response JSONValue]);
    NSDictionary *queryDic = [response JSONValue];
    NSDictionary *queryDataDic = [queryDic objectForKey:@"data"];
    newsInfos = [queryDataDic objectForKey:@"list"];
    NSLog(@"%@",newsInfos);
    //    NSLog(@"=-=-=-=%@",[queryListDic allKeys]);
    [contents removeAllObjects];
    [crttimes removeAllObjects];
    [crtusers removeAllObjects];
    [departmentids removeAllObjects];
    [titles removeAllObjects];
    [userids removeAllObjects];
    [ids removeAllObjects];
    
    for (NSDictionary *dic in newsInfos) {
        [contents addObject:[dic valueForKey:@"content"]];
        [crttimes addObject:[dic valueForKey:@"crttime"]];
        [crtusers addObject:[dic valueForKey:@"crtuser"]];
        [departmentids addObject:[dic valueForKey:@"departmentid"]];
        [titles addObject:[dic valueForKey:@"title"]];
        [userids addObject:[dic valueForKey:@"userid"]];
        [ids addObject:[dic valueForKey:@"id"]];
    }
    [mainTable reloadData];
}
-(void)requestDone:(id)sender
{
    [self loadData];
    [MBProgressHUD hideHUDForView:self.view animated:YES];
}
- (void)requestWentWrong:(id)sender
{
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"警告！！" message:@"数据请求失败！！" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alert show];
}

- (void)requestDone1:(id)sender
{
    NSString *response = [searchRequest responseString];
    NSDictionary *queryDic = [response JSONValue];
    NSLog(@"dic=%@",response);
    NSDictionary *queryDataDic = [queryDic objectForKey:@"data"];
    newsInfos = [queryDataDic objectForKey:@"list"];
    [contents removeAllObjects];
    [crttimes removeAllObjects];
    [crtusers removeAllObjects];
    [departmentids removeAllObjects];
    [titles removeAllObjects];
    [userids removeAllObjects];
    [ids removeAllObjects];
    
    for (NSDictionary *dic in newsInfos) {
        [contents addObject:[dic valueForKey:@"content"]];
        [crttimes addObject:[dic valueForKey:@"crttime"]];
        [crtusers addObject:[dic valueForKey:@"crtuser"]];
        [departmentids addObject:[dic valueForKey:@"departmentid"]];
        [titles addObject:[dic valueForKey:@"title"]];
        [userids addObject:[dic valueForKey:@"userid"]];
        [ids addObject:[dic valueForKey:@"id"]];
    }
  
    [mainTable reloadData];
    
}

#pragma mark - end
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
